package IoLianxi;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Iolianxi {
    private String str;

    //拷贝
    public void copy() {

        FileReader fr = null;  //字符串输入
        FileOutputStream fos = null; //输出


        try {
            fr = new FileReader("e:/223.txt");  //输入地址
            fos = new FileOutputStream("e:/224.txt");  //拷贝地址
            int d = 0;
            while ((d = fr.read()) != -1) {
                this.str = String.valueOf(((char) d)); //用字符char环绕，赋值给str
                fos.write(str.getBytes(StandardCharsets.UTF_8)); //把得到的文字，赋给fos

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close(); //关闭流
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //添加
    public void add() {
        System.out.println("添加文字");
        Scanner scan = new Scanner(System.in);
        String add = scan.next();
        FileWriter fw = null;  //字符串输出流
        try {
            fw = new FileWriter("e:/224.txt", true);
            fw.append(add);  //添加文字在末端
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        new Iolianxi().copy();
        new Iolianxi().add();

    }
}